<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'imprest.add'"></el-button>
                <!-- <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del" v-auth="'custom.delete'"></el-button> -->
                <!-- <sc-file-import :apiObj="$API.order.entrust.orderUp" @success="success" ref="importBox" templateUrl="https://2156.mzth.cn/import.xlsx"></sc-file-import> -->
            </div>
            <div class="right-panel">
                <div class="right-panel-search" style="flex-wrap:wrap;">
                    <el-date-picker v-model="search.apply_time" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 230px" :shortcuts="shortcuts" />
                    <el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh">重置</el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" remoteSort remoteFilter border>
                <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                <el-table-column label="申请单号" prop="reserve_code" align="center">
                    <template #default="scope">
                        <el-link type="primary" style="font-size:12px" @click="table_reserve(scope.row)">{{ scope.row.reserve_code }}</el-link>
                    </template>
                </el-table-column>
                <el-table-column label="项目编号" prop="project.project_code" align="center"></el-table-column>
                <el-table-column label="会议名称" prop="project.metting" align="center"></el-table-column>
                <el-table-column label="申请事由" prop="apply_reason" align="center"></el-table-column>
                <el-table-column label="申请金额" prop="apply_money" align="center"></el-table-column>
                <el-table-column label="已核销金额" prop="verification_money" align="center"></el-table-column>
                <el-table-column label="未核销金额" prop="not_verification_money" align="center"></el-table-column>
                <el-table-column label="核销凭证" prop="verification_voucher" align="center">
                    <template #default="scope">
                        <el-button type="text" size="small" @click="showFile(scope.row)" v-if="scope.row.verification_voucher">查看附件</el-button>
                        <!-- <el-image class="img" :src="scope.row.goods_image" :preview-src-list="scope.row.goods_image_item" fit="cover" preview-teleported="true" hide-on-click-modal /> -->
                    </template>
                </el-table-column>
                <el-table-column label="申请人" prop="admin.name" align="center"></el-table-column>
                <el-table-column label="申请时间" prop="apply_time" align="center"></el-table-column>
                <el-table-column label="申请状态" prop="apply_status" align="center">
                    <template #default="scope">
                        <el-tag v-if="scope.row.apply_status == 0" type="danger">未通过</el-tag>
                        <el-tag v-if="scope.row.apply_status == 1" type="primary">申请通过</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="核销状态" prop="verification_status" align="center">
                    <template #default="scope">
                        <el-tag v-if="scope.row.verification_status == 0" type="danger">未核销</el-tag>
                        <el-tag v-if="scope.row.verification_status == 1" type="primary">核销完成</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="210">
                    <template #default="scope">
                        <el-button-group>
                            <el-button text type="success" size="small" v-auth="'imprestcheck.check'" v-if="scope.row.apply_status == 0" @click="table_check(scope.row)">审核</el-button>
                            <el-button text type="primary" size="small" @click="table_apply_export(scope.row, scope.$index)" v-auth="'imprestcheck.apply'">申请单导出</el-button>
                            <el-button text type="warning" size="small" @click="table_back_export(scope.row, scope.$index)" v-if="scope.row.verification_status == 1" v-auth="'imprestcheck.back'">返还单导出</el-button>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></save-dialog>
    <verification v-if="dialog.verifica" ref="verificaDialog" @success="handleSuccess" @closed="dialog.verifica = false" draggable :query="{reserve_fund_id:this.reserve_fund_id}"></verification>
    <status v-if="dialog.status" ref="statusDialog" @success="handleSuccess" @closed="dialog.status = false"></status>
    <imprest-file v-if="dialog.file" ref="fileDialog" @success="handleSuccess" @closed="dialog.file = false" draggable></imprest-file>
</template>

<script>
import Verification from '@/views/project/Imprest/verification';
import saveDialog from './save.vue'
import Status from './status.vue';
import ImprestFile from '@/views/project/Imprest/imprestFile.vue';

export default {
	name: '客户管理',
	components: {
		saveDialog,Verification,Status,ImprestFile,
	},
	data() {
		return {
			dialog: {
				save: false,
                verifica: false,
                status: false,
                file: false,
			},
			apiObj: this.$API.project.Imprest.list,
			selection: [],
			search: {
				apply_time: null,
			},
            groupId:"0",
            reserve_fund_id:"",
            shortcuts:[
                {
                    text:'本周',
                    value: () => {
                        const now = new Date();
                        const dayOfWeek = now.getDay(); // 0 表示周日，1 表示周一，以此类推
                        const start = new Date(now.getFullYear(), now.getMonth(), now.getDate() - dayOfWeek + 1);
                        const end = new Date(now.getFullYear(), now.getMonth(), now.getDate() + (7 - dayOfWeek));
                        return [start, end]
                    },
                },
                {
                    text:'上周',
                    value: () => {
                        const end = new Date()
                        const start = new Date()
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
                        return [start, end]
                    },
                },
                {
                    text:'本月',
                    value: () => {
                        const now = new Date();
                        const start = new Date(now.getFullYear(), now.getMonth(), 1);
                        const end = new Date(now.getFullYear(), now.getMonth() + 1, 0);
                        return [start, end];
                    },
                },
                {
                    text:'本年',
                    value: () => {
                        const now = new Date();
                        const start = new Date(now.getFullYear(), 0, 1);
                        const end = new Date(now.getFullYear(), 11, 31);
                        return [start, end];
                    },
                },
                {
                    text:'上月',
                    value: () => {
                        const end = new Date()
                        const start = new Date()
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
                        return [start, end]
                    },
                },
                {
                    text:'本季度',
                    value: () => {
                        const end = new Date()
                        const start = new Date()
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
                        return [start, end]
                    },
                },
                {
                    text:'半年',
                    value: () => {
                        const end = new Date()
                        const start = new Date()
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 180)
                        return [start, end]
                    },
                },
                {
                    text:'上年',
                    value: () => {
                        const end = new Date()
                        const start = new Date()
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 360)
                        return [start, end]
                    },
                },
            ],
		}
	},
	mounted() {
	},
	methods: {
        table_reserve(row) {
            this.reserve_fund_id = row.id
			this.dialog.verifica = true
			this.$nextTick(() => {
				this.$refs.verificaDialog.open('edit').setData(row)
			})
		},
        // 审核
        async table_check(row) {
            this.dialog.status = true
			this.$nextTick(() => {
				this.$refs.statusDialog.open('edit').setData(row)
			})
			// var reqData = {id: row.id,apply_status:1}
			// var res = await this.$API.project.Imprest.check.post(reqData);
			// if (res.code == 1) {
            //     this.$refs.table.refresh()
			// 	this.$message.success("操作成功")
			// } else {
			// 	this.$alert(res.message, "提示", {type: 'error'})
			// }
		},
        showFile(row){
            this.dialog.file = true
			this.$nextTick(() => {
				this.$refs.fileDialog.open('show').setData(row)
			})
        },
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        refresh(){
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
	}
}
</script>

<style>
.titleBox {
	font-size: 14px;
	font-weight: 500;
	color: #0099ff;
	margin-left: 30px;
}
</style>
